package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.example.domain.dto.ShoppingCartInfo;
import org.example.domain.dto.ShoppingCartNumInfo;
import org.example.domain.entity.ProductId;
import org.example.domain.entity.ShoppingCart;
import org.example.domain.entity.UserName;
import org.example.domain.model.ShoppingCartAllInfo;
import org.example.mapper.LoginNameMapper;
import org.example.mapper.ShoppingCartMapper;
import org.example.service.IShoppingCartAllInfoService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartAllInfoServiceImpl implements IShoppingCartAllInfoService {

    //图片访问前缀
    private static final String url="https://fzlbkt.oss-cn-hangzhou.aliyuncs.com/exampledir/";

    //购物车实体类的Mapper
    private ShoppingCartMapper shoppingCartMapper;
    //用户名的有关Mapper
    private LoginNameMapper loginNameMapper;
    //查询有关商品的详情（商品描述和图片）
    private FmFrontedGoodsServiceImpl fmFrontedGoodsService;

    public ShoppingCartAllInfoServiceImpl(ShoppingCartMapper shoppingCartMapper, LoginNameMapper loginNameMapper, FmFrontedGoodsServiceImpl fmFrontedGoodsService) {
        this.shoppingCartMapper = shoppingCartMapper;
        this.loginNameMapper = loginNameMapper;
        this.fmFrontedGoodsService = fmFrontedGoodsService;
    }

    @Override
    //将指定用户的购物车查询出来并通过ShoppingCartAllInfo类进行封装
    public List<ShoppingCartAllInfo> selectAllShoppingCart(String userName) {
        QueryWrapper<UserName> userNameQueryWrapper = new QueryWrapper<>();
        userNameQueryWrapper.eq("username",userName);
        UserName userName1 = loginNameMapper.selectOne(userNameQueryWrapper);
        List<ShoppingCartAllInfo> shoppingCartAllInfoList = new ArrayList<>();
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",userName1.getUserId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(shoppingCartQueryWrapper);
        shoppingCartList.forEach(shoppingCart -> {
            //将shoppingCart类中信息详细化封装为ShoppingCartAllInfo
            shoppingCartAllInfoList.add(seal(shoppingCart));
        });
        return shoppingCartAllInfoList;
    }

    @Override
    public List<ShoppingCartAllInfo> selectAllShoppingCartToShoppingId(String shoppingId) {
        String[] split = shoppingId.split(",");
        List<ShoppingCartAllInfo> list = new ArrayList<>();
        for (int i=0;i<split.length;i++){
            ShoppingCartAllInfo shoppingCartAllInfo = selectOneShoppingCart(Integer.parseInt(split[i]));
            list.add(shoppingCartAllInfo);
        }
        return list;
    }

    @Override
    //通过购物车的id查询购物车的详细信息
    public ShoppingCartAllInfo selectOneShoppingCart(Integer id){
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("cart_id",id);
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartQueryWrapper);
        return seal(shoppingCart);
    }


    @Override
    public ShoppingCart selectOne(Integer id){
          return shoppingCartMapper.selectById(id);
    }

    @Override
    //增加购物车接口
    public int insertShoppingCart(ShoppingCartInfo shoppingCartInfo) {
        QueryWrapper<UserName> userNameQueryWrapper = new QueryWrapper<>();
        userNameQueryWrapper.eq("username", shoppingCartInfo.getUserName());
        UserName userName = loginNameMapper.selectOne(userNameQueryWrapper);
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("product_id",shoppingCartInfo.getProductId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(shoppingCartQueryWrapper);
        boolean flag = false;
        ShoppingCart shoppingCart1 = null;
        if(shoppingCartList != null){
            for (ShoppingCart shoppingCart : shoppingCartList) {
                if(shoppingCart.getUserId() == userName.getUserId() ){
                    flag = true;
                    if(shoppingCart.getUserId() == userName.getUserId()){
                        shoppingCart1 = shoppingCart;
                    }
                }
            }
        }

        //如果购物车中存在相同商品且是同一个用户就增加该商品的数量，否则就添加一个新的购物车
        if(shoppingCartList != null && flag){
            UpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new UpdateWrapper<>();
            shoppingCartUpdateWrapper.eq("product_id",shoppingCartInfo.getProductId());
            shoppingCartUpdateWrapper.eq("user_id",userName.getUserId());
            shoppingCartUpdateWrapper.set("cart_num",shoppingCart1.getCartNum()+shoppingCartInfo.getCartNum());
            int update = shoppingCartMapper.update(shoppingCart1, shoppingCartUpdateWrapper);
            System.out.println("927359urhgiueyt93yhdksghfs"+ update);
            if(update == 1){
                return userName.getUserId();
            }
            return 0;
        }else {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setProductId(shoppingCartInfo.getProductId());
            shoppingCart.setSkuId(shoppingCartInfo.getSkuId());
            shoppingCart.setUserId(userName.getUserId());
            shoppingCart.setCartNum(shoppingCartInfo.getCartNum());
            shoppingCart.setCartTime(new Date());
            shoppingCart.setProductPrice(shoppingCartInfo.getProductPrice());
            shoppingCart.setSkuProps(shoppingCartInfo.getSkuProps());
            int insert = shoppingCartMapper.insert(shoppingCart);
            if(insert == 1){
                return userName.getUserId();
            }
            return 0;
        }

    }

    @Override
    public int addShoppingCartNum(Integer shoppingCartId) {
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("cart_id", shoppingCartId);
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartQueryWrapper);
        UpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new UpdateWrapper<>();
        shoppingCartUpdateWrapper.eq("cart_id",shoppingCart.getCartId());
        shoppingCartUpdateWrapper.set("cart_num",shoppingCart.getCartNum()+1);
        int update = shoppingCartMapper.update(shoppingCart, shoppingCartUpdateWrapper);
        System.out.println(update);
        return update;
    }

    @Override
    public int minusShoppingCartNum(Integer shoppingCartId) {
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("cart_id", shoppingCartId);
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartQueryWrapper);
        UpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new UpdateWrapper<>();
        shoppingCartUpdateWrapper.eq("cart_id",shoppingCart.getCartId());
        shoppingCartUpdateWrapper.set("cart_num",shoppingCart.getCartNum()-1);
        int update = shoppingCartMapper.update(shoppingCart, shoppingCartUpdateWrapper);
        System.out.println(update);
        return update;
    }

    @Override
    public int assignShoppingCartNum(ShoppingCartNumInfo shoppingCartNumInfo) {
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("cart_id", shoppingCartNumInfo.getShoppingCartId());
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartQueryWrapper);
        UpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new UpdateWrapper<>();
        shoppingCartUpdateWrapper.eq("cart_id",shoppingCart.getCartId());
        shoppingCartUpdateWrapper.set("cart_num",shoppingCartNumInfo.getChangeNum());
        int update = shoppingCartMapper.update(shoppingCart, shoppingCartUpdateWrapper);
        System.out.println(update);
        return update;
    }

    @Override
    public int deleteShoppingCart(Integer shoppingCartId) {
        return shoppingCartMapper.deleteById(shoppingCartId);
    }

    @Override
    public int deleteCheckedShoppingCart(String checkedShoppingCartId) {
        String[] split = checkedShoppingCartId.split(",");
        List<Integer> cartId = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            cartId.add(Integer.parseInt(split[i]));
        }
        int i = shoppingCartMapper.deleteBatchIds(cartId);
        return i;
    }

    @Override
    public int countShoppingCart(String userName) {
        QueryWrapper<UserName> userNameQueryWrapper = new QueryWrapper<>();
        userNameQueryWrapper.eq("username", userName);
        UserName userName1 = loginNameMapper.selectOne(userNameQueryWrapper);
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",userName1.getUserId());

        return shoppingCartMapper.selectCount(shoppingCartQueryWrapper);
    }


    private ShoppingCartAllInfo seal(ShoppingCart shoppingCart){
        ProductId productId = fmFrontedGoodsService.selectShoppingCartProduct(shoppingCart.getProductId());
        ShoppingCartAllInfo shoppingCartAllInfo = new ShoppingCartAllInfo();
        shoppingCartAllInfo.setTitle(productId.getProductName());
        String img = url;
        if(productId.getProductSku().getSkuImg() != null){
            img += productId.getProductSku().getSkuImg();
        } else {
            img += productId.getProductImg().getUrl();
        }
        QueryWrapper<UserName> userNameQueryWrapper = new QueryWrapper<>();
        userNameQueryWrapper.eq("user_id",shoppingCart.getUserId());
        UserName userName = loginNameMapper.selectOne(userNameQueryWrapper);
        shoppingCartAllInfo.setId(shoppingCart.getCartId());
        shoppingCartAllInfo.setName(userName.getUsername());
        shoppingCartAllInfo.setPic(img);
        shoppingCartAllInfo.setNum(shoppingCart.getCartNum());
        shoppingCartAllInfo.setPrice(shoppingCart.getProductPrice());
        return shoppingCartAllInfo;
    }
}
